package org.inspursc.s0517.health.evaluation.controller;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.catalina.util.RequestUtil;
import org.inspursc.s0517.health.common.entity.SysUser;
import org.inspursc.s0517.health.common.enums.UserTypeEnum;
import org.inspursc.s0517.health.common.service.FileDealService;
import org.inspursc.s0517.health.common.util.LogUtil;
import org.inspursc.s0517.health.common.util.ResponseUtil;
import org.inspursc.s0517.health.common.util.SpringSecurityUtils;
import org.inspursc.s0517.health.common.vo.ResponseVO;
import org.inspursc.s0517.health.evaluation.dto.CustomerDTO;
import org.inspursc.s0517.health.evaluation.dto.SurveyAnswerDTO;
import org.inspursc.s0517.health.evaluation.dto.SurveyAnswerDateDTO;
import org.inspursc.s0517.health.evaluation.dto.SurveyAnswerGradeDTO;
import org.inspursc.s0517.health.evaluation.service.CustomerService;
import org.inspursc.s0517.health.evaluation.service.SurveyAnswerService;

import org.inspursc.s0517.health.evaluation.utils.ReadTextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


/**
 * @author B_Dream
 * @date 2018/6/4 15:50
 */
@RestController
@Api(description = "问卷回答api")
@RequestMapping(value = "/surveyAnswer")
public class SurveyAnswerController {

    @Autowired
    SurveyAnswerService answerService;

    @Autowired
    FileDealService fileDealService;

    @Autowired
    LogUtil logUtil;

    @Autowired
    CustomerService customerService;

    @ApiOperation("回答问卷")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResponseEntity<ResponseVO> saveQuestionnaire(@RequestBody @Valid SurveyAnswerDTO answerDTO, BindingResult bindingResult, HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
//            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
            return ResponseUtil.error400(bindingResult.getFieldError().getDefaultMessage());
        }
        try {
            SurveyAnswerGradeDTO dto = answerService.saveAnswer(answerDTO, request);
            CustomerDTO customerDTO=customerService.findById(answerDTO.getPersonnelId());
            logUtil.createLog(request,customerDTO.getName(),answerDTO.getSurveyName());
            return ResponseUtil.success("提交成功", dto);
        } catch (Exception e) {
            return ResponseUtil.error(e.getMessage(), null);
        }

    }

    @ApiOperation("回答问卷")
    @RequestMapping(value = "/uploadOffLineSurvey", method = RequestMethod.POST)
    public ResponseEntity<ResponseVO> uploadOffLineSurvey(HttpServletRequest request) {
        String txt = ReadTextUtil.readTextToStr(request);
        SurveyAnswerDTO answerDTO = JSON.parseObject(txt, SurveyAnswerDTO.class);
        try {
            SurveyAnswerGradeDTO dto = answerService.saveAnswer(answerDTO, request);
            return ResponseUtil.success("提交成功", dto);
        } catch (Exception e) {
            return ResponseUtil.error(e.getMessage(), null);
        }
    }


    @ApiOperation("查询回答问卷")
    @RequestMapping(value = "/findOneBySuIdAndPerId", method = RequestMethod.GET)
    public ResponseEntity<ResponseVO> getQuestionnaireBySuIdAndPerId(@ApiParam("分类Id") @RequestParam(value = "suId") Integer suId, @RequestParam(value = "perId") Integer perId) {

        return ResponseUtil.success("ok", answerService.findOneBySuAndPer(suId, perId));
    }

    @ApiOperation("g根据问卷问答详情Id查询回答问卷")
    @RequestMapping(value = "/findOneById", method = RequestMethod.GET)
    public ResponseEntity<ResponseVO> getQuestionnaireById(@ApiParam("回答问卷Id") @RequestParam(value = "id") Integer id) {
        try {
            SurveyAnswerGradeDTO dto = answerService.findOneById(id);
            return ResponseUtil.success(dto);
        } catch (Exception e) {
            return ResponseUtil.error(e.getMessage());
        }
    }

    @ApiOperation("根据问卷问答Id 获取客户所答问卷详情")
    @RequestMapping(value = "/getAnswerDetailById", method = RequestMethod.GET)
    public ResponseEntity<ResponseVO> getAnswerDetailById(@ApiParam("回答问卷Id") @RequestParam(value = "id") Integer id, HttpServletRequest request) {
        try {
            SurveyAnswerDateDTO dto = answerService.getAnswerDetailById(id, request);
            return ResponseUtil.success(dto);
        } catch (Exception e) {
            return ResponseUtil.error(e.getMessage());
        }
    }

    @ApiOperation("删除指定评估报告")
    @RequestMapping(value = "/deleteAnswerById", method = RequestMethod.GET)
    public ResponseEntity<ResponseVO> deleteAnswerById(@ApiParam("调查报告Id") @RequestParam(value = "id") Integer id, HttpServletRequest request) {
        SysUser user = SpringSecurityUtils.getCurrentUser(request);
        // 如果不是系统管理员
        if ( !UserTypeEnum.ADMIN.getCode().equals(user.getUserType())) {
            return ResponseUtil.error("只允许系统管理员删除评估信息！");
        }

        try {
            if(answerService.deleteAnswerById(id)){
                return ResponseUtil.success("删除成功",true);
            } else {
                return ResponseUtil.error("删除失败！");
            }
        } catch (Exception e) {
            return ResponseUtil.error("删除失败！" + e.getMessage());
        }
    }

}
